<?
require_once('./lib/authentication.lib.php');
?>

<HTML>
<HEAD><TITLE>Insert player</TITLE>
<SCRIPT TYPE="text/javascript" SRC="js/game.js"></SCRIPT>
<SCRIPT TYPE="text/javascript" SRC="js/insert_player.js"></SCRIPT>
<link rel="stylesheet" href="css/style.css" type="text/css">
</HEAD>

<BODY>

	<FORM ACTION="players.php" METHOD="POST" NAME="existing">

<?
if (isset($_POST["player_id1"]) and isset($_POST["player_id2"]))
{
	$id1 = mysql_escape_string($_POST["player_id1"]);
	$id2 = mysql_escape_string($_POST["player_id2"]);

	if ($id1 == $id2)
	{
		echo "<H3>Same player</H3>\n";
	}
	else
	{
		$query = "UPDATE Goal SET player = '$id1' WHERE player = '$id2';";
		mysql_query($query) or die("Can't update player");

		$query = "UPDATE player_game SET player = '$id1' WHERE player = '$id2';";
		mysql_query($query) or die("Can't update player");

		# This may fail because the other player may be from the same team
		$query = "UPDATE team_player SET player = '$id1' WHERE player = '$id2';";
		mysql_query($query) or die("Can't update player - $query");

		$query = "DELETE FROM Player WHERE id = '$id2';";
		mysql_query($query) or die("Can't update player");

		echo "<H3>Player merged</H3>\n";
	}
}
	
	$query = "SELECT id, name, position, full_name FROM Player ORDER BY name;";
	$result_player = mysql_query($query);
	$num_player = mysql_numrows($result_player);
	for ($i = 0; $i < $num_player; $i++)
	{
		$player_id = mysql_result($result_player, $i, "id");
		$player_name = mysql_result($result_player, $i, "name");
		$player_full = mysql_result($result_player, $i, "full_name");
		$player_pos = mysql_result($result_player, $i, "position");

		$players_str .= "<OPTION VALUE='$player_id'>$player_name ($player_pos) - $player_full</OPTION>\n";
	}
?>
	<TABLE>
	<TR>
	<TD> <DIV ID='player_info1'></DIV> </TD>
	<TD> <DIV ID='player_info2'></DIV> </TD>
	</TR>
	<TR>
	<TD> <SELECT NAME="player_id1" onChange="show_player_info(this, document.getElementById('player_info1'));">
	<? echo $players_str; ?>
	</SELECT> </TD>
	<TD> <SELECT NAME="player_id2" onChange="show_player_info(this, document.getElementById('player_info2'));">
	<? echo $players_str; ?>
	</SELECT> </TD>
	</TR>
	</TABLE>

	<INPUT TYPE="submit" NAME="submit" value="Send">


